package com.yw.controller;

import com.yw.ScriptInfo.domain.PythonScriptRequest;
import com.yw.ScriptInfo.dto.AddScriptDTO;
import com.yw.ScriptInfo.dto.QueryScriptDTO;
import com.yw.piliangDTO.StatusDTO;
import com.yw.result.R;
import com.yw.service.ScriptService;
import com.yw.utils.PythonScriptDownloader;
import com.yw.utils.PythonScriptExecutor;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin(origins = "*")  //解决跨域
@Tag(name = "脚本管理",description = "脚本管理")
@RequestMapping("/script")
public class ScriptController {
    @Resource
    private ScriptService scriptService;

    private static final String UPLOAD_DIR = "E:/script/";

    @Operation(summary = "条件分页查询接口表")
    @PostMapping("/pageAll")
    public R pageScript(@RequestBody QueryScriptDTO queryScriptDTO){
        return R.OK(scriptService.pageScript(queryScriptDTO));
    }
    //    批量发布停用
    @Operation(summary = "批量发布停用")
    @PostMapping("/batch")
    public R batch(@RequestBody StatusDTO statusDTO){
        return scriptService.batch(statusDTO)?R.OK("批量发布成功"):R.FAIL("批量发布失败");
    }

    @Operation(summary = "修改接口状态")
    @PostMapping("/updateStatic")
    public R updateStatic(Integer id){
        return scriptService.updateStatic(id)?R.OK():R.FAIL();
    }

    @Operation(summary = "删除脚本")
    @PostMapping("/deleteScript")
    public R deleteScript(Integer id){
        return scriptService.removeById(id)?R.OK():R.FAIL();
    }

    @Operation(summary = "根据名字返回详情和回显数据")
    @PostMapping("/getScriptByName")
    public R getScriptByName(Integer id){
        return R.OK(scriptService.getScriptByName(id));
    }

    @Operation(summary = "保存脚本")
    @PostMapping("/saveScript")
    public R saveScript(@RequestBody AddScriptDTO addScriptDTO){
        return scriptService.addScript(addScriptDTO)?R.OK():R.FAIL();
    }

    @Operation(summary = "编辑脚本")
    @PostMapping("/updateScript")
    public R updateScript(@RequestParam Integer id,@RequestBody AddScriptDTO addScriptDTO){
        return scriptService.updateScript(id,addScriptDTO)?R.OK():R.FAIL();
    }
    @Operation(summary = "脚本测试")
    @PostMapping("/execute-python")
    public R executePythonScript(@RequestBody PythonScriptRequest request) {
       return scriptService.executePythonScript(request);
    }

    @Operation(summary = "任务中点击脚本下拉框显示")
    @PostMapping("/taskGetScript")
    public R taskGetScript(){
        return R.OK(scriptService.taskGetScript());
    }

    @Operation(summary = "任务中中返回脚本输入输出参数")
    @PostMapping("/getValues")
    public R getValues(Integer id){
        return R.OK(scriptService.getValues(id));
    }

    @Operation(summary = "脚本文件上传")
    @PostMapping(value = "/insertFile",consumes = "multipart/form-data")
    public R uploadPythonScript(@RequestParam("file") MultipartFile file) {
        return scriptService.uploadPythonScript(file);
    }


}
